package com.llf.Blog.ServiceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.llf.Blog.Pojo.User;
import com.llf.Blog.Mapper.UserMapper;
import com.llf.Blog.Service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Wrapper;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author llf
 * @since 2021-08-07
 */
@Transactional(rollbackFor=Exception.class)
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    UserMapper userMapper;

    @Override
    public User getUser(String username) {
       

        return userMapper.getUser(username);
    }

    @Override
    public int updatePassword(User user) {
        return userMapper.updatePassword(user.getPassword());
    }

    @Override
    public HashMap insertUser(User user) {
        user.setAvatar("/images/aoavatar.png");
        user.setRole("guest");
        String username = user.getUsername();
        User user1 = userMapper.getUser(username);
        HashMap<String, Object> map = new HashMap<>();
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("nickname",user.getNickname());
        List<User> userList = userMapper.selectList(wrapper);
        System.out.println("================================"+userList);
        if (user1==null&&userList.size()<1){
            map.put("data",userMapper.insert(user));
            return map;
        }else if (userList.size()>=1){
            map.put("data","用户昵称已存在");
            return map;
        }else {
            map.put("data","用户名已存在");
            return map;
        }



    }
}
